package com.only4play.practice.ordercenter.core.domainservice;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.only4play.codegen.support.base.EntityOperations;
import com.only4play.codegen.support.constants.CodeEnum;
import com.only4play.codegen.support.exception.BusinessException;
import com.only4play.practice.ordercenter.api.model.domainmodel.OrderItemModel;
import com.only4play.practice.ordercenter.api.model.domainmodel.OrderCompleteModel;
import com.only4play.practice.ordercenter.api.model.domainmodel.OrderCreateModel;
import com.only4play.practice.ordercenter.api.model.domainmodel.OrderReviseModel;
import com.only4play.practice.ordercenter.core.entity.OrderBase;
import com.only4play.practice.ordercenter.core.mapper.OrderBaseMapper;
import com.only4play.practice.ordercenter.entitymapper.OrderBaseObjectMapper;
import com.only4play.practice.ordercenter.facade.IFlowNoFacade;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.stream.Stream;


/**
 * @author mengdy
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderDomainServiceImpl implements IOrderDomainService{

  private final OrderBaseMapper orderBaseMapper;

  private final IFlowNoFacade flowNoFacade;

  private final ApplicationEventPublisher publisher;

  @Override
  public boolean orderCreate(OrderCreateModel createModel) {
    Assert.notEmpty(createModel.getItemInfoList());
//    Long itemTotal = createModel.getItemInfoList().stream().map(OrderItemModel::getRealAmount)
//            .reduce(0L, (a, b) -> (NumberUtil.add(a, b).longValue()));
    // 假设 createModel 和 OrderItemModel 已经定义好
    Long itemTotal = 0L;
    // 遍历订单项列表
    for (OrderItemModel item : createModel.getItemInfoList()) {
      // 累加每个订单项的金额
      itemTotal += item.getRealAmount();
    }
    Long flowNo = flowNoFacade.getNextId();
    OrderBase orderBase = OrderBaseObjectMapper.INSTANCE.model2Entity(createModel);
    orderBase.setFlowNo(flowNo);
    orderBase.setTotalAmount(itemTotal);
    EntityOperations
        .doCreate(orderBaseMapper)
        .create(() -> orderBase)
        .update(e -> e.init(createModel,e,publisher))
        .execute();
    return true;
  }

  @Override
  public boolean orderRevise(OrderReviseModel reviseModel) {

    return true;
  }

  @Override
  public boolean orderComplete(OrderCompleteModel completeModel) {
    QueryWrapper<OrderBase> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("flow_no",completeModel.getFlowNo());
    queryWrapper.last("limit 1");
    OrderBase order = orderBaseMapper.selectOne(queryWrapper);
    if(!ObjectUtil.isNotNull(order)){
      throw new BusinessException(CodeEnum.NotFindError);
    }
    EntityOperations
        .doUpdate(orderBaseMapper)
        .load(() -> order)
        .update(e -> e.complete(completeModel,e ,publisher))
        .execute();
    return true;
  }

  @Override
  public boolean orderCancel(Long flowNo) {
    QueryWrapper<OrderBase> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("flow_no",flowNo);
    queryWrapper.last("limit 1");
    OrderBase order = orderBaseMapper.selectOne(queryWrapper);
    if(!ObjectUtil.isNotNull(order)){
      throw new BusinessException(CodeEnum.NotFindError);
    }
    EntityOperations
        .doUpdate(orderBaseMapper)
        .load(() -> order)
        .update(e -> e.cancel(e))
        .execute();
    return true;
  }
}
